<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.supplier.audit.info') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="130px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-descriptions border :column="3">
                <el-descriptions-item width="200" :label="i18n('organization.chinese_title')" label-align="right">
                  {{ dataForm.chinese_title }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.tax_code')" label-align="right">
                  {{ dataForm.tax_code }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.manager_name')" label-align="right">
                  {{ dataForm.manager_name }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.manager_mobile')" label-align="right">
                  {{ dataForm.manager_mobile }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.country_id')" label-align="right">
                  {{ dataForm.country_text }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.area')" label-align="right">
                  {{ dataForm.province_id }} {{ dataForm.city_id }} {{ dataForm.region_id }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.detail.address')" label-align="right" :span="3">
                  {{ dataForm.address }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('business.supplier.business_scope')" label-align="right" :span="3">
                  {{ dataForm.business_scope }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.logo')" label-align="right">
                  <el-link v-if="dataForm.logo" :href="dataForm.logo" type="primary" target="_blank">
                    <el-image class="cx-list-image" :src="dataForm.logo">
                      <template v-slot:error>
                        <div class="image-slot">
                          <el-icon><Picture /></el-icon>
                        </div>
                      </template>
                    </el-image>
                  </el-link>
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.detail.license')" label-align="right">
                  <el-link v-if="dataForm.license" :href="dataForm.license" type="primary" target="_blank">
                    <el-image class="cx-list-image" :src="dataForm.license">
                      <template v-slot:error>
                        <div class="image-slot">
                          <el-icon><Picture /></el-icon>
                        </div>
                      </template>
                    </el-image>
                  </el-link>
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('business.supplier.audit.info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('business.supplier.type')" prop="type">
                <el-select
                  v-model="dataForm.type"
                  :placeholder="i18n('common.please_select') + i18n('business.supplier.type')" filterable clearable>
                  <el-option v-for="(v, k) in typeList" :label="v.text" :key="k" :value="v.value"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('business.supplier.start_time')" prop="start_time">
                <el-date-picker v-model="dataForm.start_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.supplier.start_time')" clearable/>
              </el-form-item>

              <el-form-item :label="i18n('business.supplier.end_time')" prop="end_time">
                <el-date-picker v-model="dataForm.end_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.supplier.end_time')" clearable/>
              </el-form-item>

              <el-form-item :label="i18n('business.supplier.harbor_code')" prop="harbor_code">
                <el-select v-model="dataForm.harbor_code" :placeholder="i18n('common.please_select') + i18n('business.supplier.harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote multiple clearable>
                  <el-option v-for="(v, k) in harborList" :label="v.chinese_name" :key="k" :value="v.code">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('business.supplier.audit_status')" prop="audit_status">
                <el-select v-model="dataForm.audit_status" :placeholder="$t('common.please_select') + $t('business.supplier.audit_status')" clearable>
                  <el-option v-for="(v, k) in auditStatusList" :label="v.text" :key="k" :value="v.value">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('business.supplier.remark')" prop="remark">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('business.supplier.remark')" v-model="dataForm.remark" rows="3" show-word-limit maxlength="200"></el-input>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-supplier-audit-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/supplier')

  const loading = ref(false)
  const typeList = ref([])
  const harborList = ref([])
  const auditStatusList = ref([])

  const dataForm = reactive({
    id: 0,
    organization_id: 0,
    tax_code: '',
    logo: '',
    chinese_title: '',
    business_scope: '',
    license: '',
    manager_name: '',
    manager_mobile: '',
    country_text: '',
    province_id: '',
    city_id: '',
    region_id: '',
    address: '',
    type: '1',
    start_time: '',
    end_time: '',
    harbor_code: [],
    audit_status: '1',
    remark: '',
  })

  const dataRule = reactive({
    audit_status: [{
      required: true,
      message: i18n('business.supplier.rules.audit_status.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.organization_id = data.data.organization_id
            dataForm.business_scope = data.data.business_scope
            dataForm.manager_name = data.data.manager_name
            dataForm.manager_mobile = data.data.manager_mobile
            dataForm.start_time = data.data.start_time
            dataForm.end_time = data.data.end_time
            dataForm.country_text = data.data.country_text.text
            dataForm.province_id = data.data.province_id.text
            dataForm.city_id = data.data.city_id.text
            dataForm.region_id = data.data.region_id.text
            dataForm.remark = data.data.remark
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/organization/data`),
              method: 'get',
              params: $http.adornParams({
                'id': dataForm.organization_id
              }),
            }).then(({ data }) => {
              if (data.data && data.status === 200) {
                dataForm.tax_code = data.data.tax_code
                dataForm.logo = data.data.logo
                dataForm.chinese_title = data.data.chinese_title

                if(data.data.detail) {
                  dataForm.license = data.data.detail.license
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'supplier_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data.data && data.status === 200) {
                dataForm.address = data.data.address
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/audit`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            type: dataForm.type,
            start_time: dataForm.start_time,
            end_time: dataForm.end_time,
            harbor_code: dataForm.harbor_code,
            audit_status: dataForm.audit_status,
            remark: dataForm.remark,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'supplier_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadHarborList = (keyword) => {
    if (keyword && keyword.length > 1) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/harbor/select'),
        method: 'get',
        params: $http.adornParams({
          keyword: keyword
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          harborList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      harborList.value = []
      loading.value = false
    }
  }

  const loadAuditStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'audit_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        auditStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadTypeList()
    loadAuditStatusList()
  })
</script>

<style lang="scss" scoped>
  .cx-list-image {
    width: 30px;
    height: 30px;
  }
</style>
